// Copyright 2013 Tumblr, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package meta implements the Vena meta shard logic.
//
//	Metric => Key => Value
//
package meta

import (
	"circuit/use/circuit"
	"sync"
	"vena"
	"vena/server"
	"vena/util"
)

type start struct{}

func init() {
	circuit.RegisterFunc(start{})
}

const ServiceName = "vena-meta"

func (start) Start(dbDir string, cacheSize int) circuit.XPerm {
	srv, err := New(dbDir, cacheSize)
	if err != nil {
		panic(err)
	}
	circuit.Listen(ServiceName, srv)
	circuit.RunInBack(func() { <-(chan int)(nil) })
	return circuit.PermRef(srv)
}

type Server struct {
	util.Server
	wlk, rlk sync.Mutex
	nwrite   int64
	nquery   int64
}

func init() {
	circuit.RegisterValue(&Server{})
}

func New(dbDir string, cacheSize int) (*Server, error) {
	t := &Server{}
	if err := t.Server.Init(dbDir, cacheSize); err != nil {
		return nil, err
	}
	return t, nil
}

func (srv *Server) Compress(tagset map[string]string) vena.TagSetID {
	…
}
